//
//  FangDangListControler.m
//  WoChuangFuMini
//
//  Created by 陈贵邦 on 15/10/16.
//  Copyright © 2015年 陈贵邦. All rights reserved.
//

#import "FangDangListControler.h"
#import "FangdangListCell.h"

@implementation FangDangListControler

- (void)viewDidLoad {
    [self setNavigationBar];
    [super viewDidLoad];
    [self initTableView];
    [self initFanDang];
}


-(void)setNavigationBar{
    self.navigationItem.title = @"返档列表";
    [self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]];
    [self.navigationController.navigationBar  setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
-(void)initTableView{
    infoTable = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    infoTable.delegate = self;
    infoTable.dataSource = self;
    [self.view addSubview:infoTable];
}

-(void)initFanDang{
    fangdangDBTool = [[FangDangDBTool alloc]init];
    [fangdangDBTool createDataBase];
    notfanArr = [[NSMutableArray alloc]init];
    infoArr = [[NSMutableArray alloc]init];
    [self requestFangdangInfo];
}

-(void)requestFangdangInfo{
    [notfanArr removeAllObjects];
    [infoArr removeAllObjects];
    
    [infoArr addObjectsFromArray:[fangdangDBTool findWithUid:@"0" limit:0]];
    for (FanDangUser *user1 in infoArr) {
        //0没有返档
        if ([user1.isfan isEqualToString:@"0"]) {
            [notfanArr addObject:user1];
        }else{
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [notfanArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FangdangListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[FangdangListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell" withFrame:CGRectMake(0, 0, self.view.frame.size.width, 125)];
    }
    FanDangUser *fuser = notfanArr[indexPath.row];
    cell.netnumLabel.text = [message hexStringDes:fuser.netnum withEncrypt:NO] ;
    cell.nameLabel.text =  [[message hexStringDes:fuser.name withEncrypt:NO] stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"*"];
    cell.idnumberLabel.text = [[message hexStringDes:fuser.idnumber withEncrypt:NO] stringByReplacingCharactersInRange:NSMakeRange(6, 8) withString:@"********"];
    cell.reamrkLabel.text =  [message hexStringDes:fuser.remark withEncrypt:NO];
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    user = [notfanArr objectAtIndex:indexPath.row];
    UIAlertView *weifan = [[UIAlertView alloc]initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"马上返档",@"查看备注",@"删除", nil];
    
    [weifan show];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 125;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView setEditing:NO animated:YES];
    FanDangUser *user2 = [notfanArr objectAtIndex:indexPath.row];
    [fangdangDBTool deleteUserWithId:user2.uid];
    [self requestFangdangInfo];
    [tableView reloadData];
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return @"请点击信息进行返档操作";
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTitle isEqualToString:@"马上返档"]) {
        [self sureFangDang];
    }else if ([buttonTitle isEqualToString:@"查看备注"]){
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"备注" message:user.remark.length>0 ? [message hexStringDes:user.remark withEncrypt:NO] : @"无相关备注" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else if ([buttonTitle isEqualToString:@"删除"]){
        [fangdangDBTool deleteUserWithId:user.uid];
        [self requestFangdangInfo];
        [infoTable reloadData];
    }else if ([buttonTitle isEqualToString:@"取消"]){
        
    }
    
    
    
    
    
}




- (void)sureFangDang {
    
    [self.view endEditing:YES];
    if (user.netnum.length <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入返档号码"];
        return;
    }
    if (user.name.length <= 0 || user.idnumber.length <= 0|| user.address.length <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请先将身份证放置读卡器上读取信息"];
        return;
    }
    bussineDataService *buss=[bussineDataService sharedDataService];
    buss.target=self;
    buss.HUDStatusString = @"返档中";
    NSString *session = [[NSUserDefaults standardUserDefaults] objectForKey:@"sessionid"];
    
    NSDictionary *SendDic = [[NSDictionary alloc] initWithObjectsAndKeys:
                             session,@"sessionId",
                             [message hexStringDes:user.netnum withEncrypt:NO]  ,@"phoneNum",
                             [message hexStringDes:user.name withEncrypt:NO]  ,@"custName",
                             [message hexStringDes:user.idnumber withEncrypt:NO]  ,@"custNo",
                             [message hexStringDes:user.address withEncrypt:NO]  ,@"custAddr",
                             [message hexStringDes:user.remark withEncrypt:NO]  ,@"remark ",
                             nil];
    [buss sureGuiDang:SendDic];
    
}

-(void)requestDidFinished:(NSDictionary *)info{
    NSString* oKCode = @"0000";
    NSString* bizCode = [info objectForKey:@"bussineCode"];
    NSString* errCode = [info objectForKey:@"errorCode"];
    
    NSString* respCode = [info objectForKey:@"respCode"];
    
    
    if([[RealFangDangMessage getBizCode] isEqualToString:bizCode])
    {
        
        bussineDataService *bus=[bussineDataService sharedDataService];
        NSLog(@"归档信息 ：%@",bus.rspInfo);
        
        
        
        if([@"0" isEqualToString:respCode])
        {
            
            
            user.isfan = @"1";
            [fangdangDBTool mergeWithUser:user];
            [self requestFangdangInfo];
            [infoTable reloadData];
            
        }
        
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:bus.rspInfo[@"respDesc"] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        //        [self ShowProgressHUDwithMessage:bus.rspInfo[@"respDesc"]];
    }
}

-(void)requestFailed:(NSDictionary *)info{
    NSString* bizCode = [info objectForKey:@"bussineCode"];
    NSString* msg = [info objectForKey:@"MSG"];
    if([[RealFangDangMessage getBizCode] isEqualToString:bizCode]){
        if(msg == nil || msg.length <= 0){
            msg = @"获取数据失败!";
        }
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"返档失败" message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        alert.tag = 10101;
        [alert show];
    }
}

@end
